#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

    setWindowTitle("Qt事件过滤器--测试程序");

    //展示图票
    label1jpg=new QLabel;
    image1jpg.load(":/res/a.jpg");
    label1jpg->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    label1jpg->setPixmap(QPixmap::fromImage( image1jpg));

    label2jpg=new QLabel;
    image2jpg.load(":/res/b.jpg");
    label2jpg->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    label2jpg->setPixmap(QPixmap::fromImage( image2jpg));

    label3jpg=new QLabel;
    image3jpg.load(":/res/c.jpg");
    label3jpg->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    label3jpg->setPixmap(QPixmap::fromImage( image3jpg));

    //UI布局
    labeldispinfo=new QLabel("鼠标按键提示信息!");
    labeldispinfo->setAlignment(Qt::AlignCenter);

    QHBoxLayout *hlayout=new QHBoxLayout;
    hlayout->addWidget(label1jpg);
    hlayout->addWidget(label2jpg);
    hlayout->addWidget(label3jpg);

    QWidget *wgt=new QWidget(this);
    QVBoxLayout *vlayout=new QVBoxLayout(wgt);
    vlayout->addLayout(hlayout);
    vlayout->addWidget(labeldispinfo);

    setCentralWidget(wgt);
    //安装事件过滤
    label1jpg->installEventFilter(this);

    label2jpg->installEventFilter(this);

    label3jpg->installEventFilter(this);

}

MainWindow::~MainWindow()
{
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if(watched==label1jpg)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            QMouseEvent *mvent=static_cast<QMouseEvent*>(event);
            if(mvent->buttons()&Qt::LeftButton)
            {
                labeldispinfo->setText("鼠标左键被按下：[左边图片]");
            }
            else if(mvent->buttons() & Qt::MidButton)
            {
                labeldispinfo->setText("鼠标中键被按下：[左边图片]");
            }
            else if(mvent->buttons() & Qt::RightButton)
            {
                labeldispinfo->setText("鼠标右键被按下：[左边图片]");
            }

            QMatrix matrix;
            matrix.scale(2,2);

            QImage tempimage=image1jpg.transformed(matrix);
            label1jpg->setPixmap(QPixmap::fromImage(tempimage));

        }
        if(event->type()==QEvent::MouseButtonRelease) // 鼠标释放
        {
            labeldispinfo->setText("鼠标按键已经释放：[左边图片]");
            label1jpg->setPixmap(QPixmap::fromImage(image1jpg));
        }
    }

    //第二张图片开始显示
    else if(watched==label2jpg)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
           QMouseEvent * mvent=static_cast<QMouseEvent*>(event);
           if(mvent->button()==Qt::LeftButton)
           {
              labeldispinfo->setText("鼠标左键被按下：[中间图片]");
           }
           else if(mvent->buttons() & Qt::MidButton)
           {
               labeldispinfo->setText("鼠标中键被按下：[中间图片]");
           }
           else if(mvent->buttons() & Qt::RightButton)
           {
               labeldispinfo->setText("鼠标右键被按下：[中间图片]");
           }
           QMatrix matrix;
           matrix.scale(2,2);
           QImage tempimage=image2jpg.transformed(matrix);
           label2jpg->setPixmap(QPixmap::fromImage(tempimage));
        }
        if(event->type()==QEvent::MouseButtonRelease)
        {
            labeldispinfo->setText("鼠标按键已经释放：[中间图片]");
            label2jpg->setPixmap(QPixmap::fromImage(image2jpg));
        }

    }
    //第三张图片开始显示
     else if(watched==label3jpg)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
           QMouseEvent * mvent=static_cast<QMouseEvent*>(event);
           if(mvent->button()==Qt::LeftButton)
           {
              labeldispinfo->setText("鼠标左键被按下：[右边图片]");
           }
           else if(mvent->buttons() & Qt::MidButton)
           {
               labeldispinfo->setText("鼠标中键被按下：[右边图片]");
           }
           else if(mvent->buttons() & Qt::RightButton)
           {
               labeldispinfo->setText("鼠标右键被按下：[右边图片]");
           }
           QMatrix matrix;
           matrix.scale(2,2);
           QImage tempimage=image3jpg.transformed(matrix);
           label3jpg->setPixmap(QPixmap::fromImage(tempimage));
        }
        if(event->type()==QEvent::MouseButtonRelease)
        {
            labeldispinfo->setText("鼠标按键已经释放：[右边图片]");
            label3jpg->setPixmap(QPixmap::fromImage(image3jpg));
        }

    }


}
